from sklearn.feature_selection import VarianceThreshold
from scipy.stats import pearsonr
import pandas as pd
import matplotlib.pyplot  as plt


def variance_demo():
    df = pd.read_csv("../data1/factor_returns.csv")
    # 截取一下特征
    df = df.iloc[:, 1:-2]
    # 1.初始化 VarianceThreshold对象
    transfer = VarianceThreshold(threshold=100)
    # 2.调用fit_transform
    data = transfer.fit_transform(df)

    print(data)
    print(data.shape)


def pearsonr_demo():
    data = pd.read_csv("../data1/factor_returns.csv")
    factor = ['pe_ratio', 'pb_ratio', 'market_cap', 'return_on_asset_net_profit', 'du_return_on_equity', 'ev',
              'earnings_per_share', 'revenue', 'total_expense']
    # result = pearsonr(df["pe_ratio"], df["pb_ratio"])
    # print(result)
    for i in range(len(factor)):
        for j in range(i, len(factor) - 1):
            print(
                "指标%s与指标%s之间的相关性大小为%f" % (factor[i], factor[j + 1], pearsonr(data[factor[i]], data[factor[j + 1]])[0]))


def paint_scatter():
    data = pd.read_csv("../data1/factor_returns.csv")
    plt.figure(figsize=(20, 8), dpi=100)
    # plt.scatter(data['revenue'], data['total_expense'])
    plt.scatter(data['pe_ratio'], data['pb_ratio'])
    plt.show()


if __name__ == '__main__':
    # variance_demo()
    # pearsonr_demo()
    paint_scatter()
